import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import { defineAsyncComponent } from 'vue'


const router = createRouter({
    history: createWebHashHistory(),  // hash 模式
    //history: createWebHistory(),  // history 模式
    routes: [
        {
            path: '/login',
            name: 'login',
            component: () => import("../views/LoginNew.vue"),
            meta: {
                title: '系统登录',
            },
        },
        {
            path: '/login1',
            name: 'login1',
            component: () => import("../views/Login.vue"),
            meta: {
                title: '系统登录',
            },
        },
        {
            path: '/screen',
            name: 'screen',
            component: () => import("../views/BigScreen.vue")
        },
        {
            path: '/',
            name: 'home',
            component: () => import("../views/Home.vue"),
            redirect: "center",
            children: [
                {
                    path: '/center',
                    name: 'center',
                    component: () => import("../views/Center.vue"),

                },
                {
                    path: '/alarm',
                    name: 'alarm',
                    component: () => import("../views/Alarm.vue"),
                },
                
                {
                    path: '/task',
                    name: 'task',
                    component: () => import("../views/Task.vue"),
                },
                {
                    path: '/train',
                    name: 'train',
                    component: () => import("../views/ModelTrain.vue"),
                },
                {
                    path: '/system',
                    name: 'system',
                    redirect: "/system/userinfo",
                    component: () => import("../views/System.vue"),
                    children: [
                        {
                            path: 'config',
                            name: 'config',
                            component: () => import("../views/system/ConfigManager.vue")
                        },
                        {
                            path: 'userinfo',
                            name: 'userinfo',
                            component: () => import("../views/system/UserManager.vue")
                        },
                        {
                            path: 'media',
                            name: 'media',
                            component: () => import("../views/Media.vue"),
                        },
                    ]
                }
            ],
            meta: {
                title: '主页',
            },
        }
    ]
})


router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        next();
    } else {
        let token = sessionStorage.getItem('token');
        //console.log(token);
        if (token === null || token === '') {
            next('/login');
        } else {
            next();
        }
    }
});


// router.beforeEach((to, from, next) => {
//     if (to.path === '/login') {
//         next();
//     } else {
//         let token = sessionStorage.getItem('Authorization');
//         //console.log(token);
//         if (token === null || token === '') {
//             next('/login');
//         } else {
//             next();
//         }
//     }
// });


export default router
